/**
 *
 */
package mblog.web.controller.desk.posts;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import mblog.core.biz.PostBiz;
import mblog.core.data.Attach;
import mblog.core.data.Group;
import mblog.core.data.Post;
import mblog.core.persist.service.AttachService;
import mblog.core.persist.service.GroupService;
import mblog.web.controller.BaseController;
import mblog.web.controller.desk.Views;
import mtons.modules.pojos.Data;
import mtons.modules.pojos.UserProfile;

/**
 * 文章操作
 * @author langhsu
 *
 */
@Controller
@RequestMapping("/post")
public class PostController extends BaseController {
	@Autowired
	private PostBiz postPlanet;
	@Autowired
	private GroupService groupService;
	@Autowired
	private AttachService attachService;

	/**
	 * 发布文章页
	 * @param groupKey
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/new/{groupKey}", method = RequestMethod.GET)
	public String view(@PathVariable String groupKey, ModelMap model) {
		Group group = groupService.getByKey(groupKey);
		model.put("group", group);
		return routeView(Views.ROUTE_POST_PUBLISH, group.getTemplate());
	}

	/**
	 * 提交发布
	 * @param blog
	 * @return
	 */
	@RequestMapping(value = "/submit", method = RequestMethod.POST)
	public String post(Post blog, HttpServletRequest request) {

		if (blog != null && StringUtils.isNotBlank(blog.getTitle())) {
			UserProfile profile = getSubject().getProfile();

			String[] ablums = request.getParameterValues("delayImages");
			blog.setAlbums(handleAlbums(ablums));
			blog.setAuthorId(profile.getId());

			postPlanet.post(blog);
		}
		return Views.REDIRECT_HOME_POSTS;
	}
	@RequestMapping(value={"/submit2"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
	  public String post2(Post blog, HttpServletRequest request)
	  {
		String[] USERS = new String[]{"2","3","4","6","7","9","10","11","12","13","14","16","18","19","20","21","28","29","30","31","36","37","38","39","40","42","43","44","45","46","48","49","50","51","52","53","55","57","58","61","62","66","67","68","69","70","71","74","75","77","78","85","86","87","88","89","91","93","94","97","98","99","100","105","107","260","391","399","410"};
		Random r = new Random();  
		int randomKey = r.nextInt(USERS.length);
	    if ((blog != null) && (StringUtils.isNotBlank(blog.getTitle()))) {
	      UserProfile profile = getSubject().getProfile();
	      String[] ablums = request.getParameterValues("delayImages");
	      blog.setAlbums(handleAlbums(ablums));
	      blog.setAuthorId(Long.parseLong(USERS[randomKey]));

	      this.postPlanet.post(blog);
	    }
	    return "redirect:/home?method=posts";
	  }
	
	@Test
	public  void main(String[] args) {
		Post blog = new Post();
		List<Attach> albums;
		
		blog.setTitle("titleTest");
		setContent(blog);
	}
	public void setContent(Post blog){
		System.out.println(blog);
		if (blog != null && StringUtils.isNotBlank(blog.getTitle())) {
			UserProfile profile = getSubject().getProfile();
	
			String[] ablums = null;
			blog.setAlbums(handleAlbums(ablums));
			blog.setAuthorId(profile.getId());
	
			postPlanet.post(blog);
		}
	}
	/**
	 * 删除文章
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public @ResponseBody Data delete(@PathVariable Long id) {
		Data data = Data.failure("操作失败");
		if (id != null) {
			UserProfile up = getSubject().getProfile();
			try {
				postPlanet.delete(id, up.getId());
				data = Data.success("操作成功", Data.NOOP);
			} catch (Exception e) {
				data = Data.failure(e.getMessage());
			}
		}
		return data;
	}

	/**
	 * 修改文章
	 * @param id
	 * @return
	 */
	@RequestMapping("/to_update/{id}")
	public String toUpdate(@PathVariable Long id, ModelMap model) {
		UserProfile up = getSubject().getProfile();
		Post ret = postPlanet.getPost(id);

		Assert.notNull(ret, "该文章已被删除");

		Assert.isTrue(ret.getAuthorId() == up.getId(), "该文章不属于你");
		Group group = groupService.getById(ret.getGroup());

		model.put("view", ret);
		model.put("group", group);
		return routeView(Views.ROUTE_POST_UPDATE, group.getTemplate());
	}

	/**
	 * 更新文章方法
	 * @author LBB
	 * @return
	 */
	@RequestMapping(value = "/update/{groupKey}", method = RequestMethod.POST)
	public String subUpdate(Post p, HttpServletRequest request) {
		UserProfile up = getSubject().getProfile();
		if (p != null && p.getAuthorId() == up.getId()) {
			String[] ablums = request.getParameterValues("delayImages");
			p.setAlbums(handleAlbums(ablums));
			postPlanet.update(p);
		}
		return Views.REDIRECT_HOME_POSTS;
	}

	@RequestMapping("/delete_album")
	public @ResponseBody Data deleteAlbum(Long id) {
		Data data = Data.failure("操作失败");
		if (id != null) {
			try {
				attachService.delete(id);
				data = Data.success("操作成功", Data.NOOP);
			} catch (Exception e) {
				data = Data.failure(e.getMessage());
			}
		}
		return data;
	}


}
